Package gri.tasks.gui

Source Code of gri.tasks.gui.TaskHeaderPanel

/*
* File: TaskHeaderPanel.java
* Author: Daniel Rogers
* Created on Apr 11, 2008
*
*/
package gri.tasks.gui;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

import gri.tasks.managers.ExtendedTaskDescription;

import gri.tasks.managers.TaskDescription;


import dan.web.BrowserLauncher;
import dan.swing.JClickLabel;
import dan.swing.JPrettyPanel;

/**
* Panel to display task information.
*
* @author dan.rogers
*/
public class TaskHeaderPanel extends JPrettyPanel {

  TaskDescription taskDescription;

  public TaskHeaderPanel(TaskDescription taskDescription) {
    super(new BorderLayout());
    init();

    if (taskDescription != null)
      setTaskDescription(taskDescription);
  }
  public TaskHeaderPanel() {
    this(null);
  }

  // ------------------------------------------------- GUI

  JLabel lblIcon;

  JPanel pnlLabels;
  StringDisplay lblTaskName;
  StringDisplay lblDescription;
  StringLinkDisplay lblPackage;

  protected void init() {
    lblIcon = new JLabel("", JLabel.CENTER);
    lblIcon.setPreferredSize(new Dimension(50,50));
    add(lblIcon, BorderLayout.WEST);

    pnlLabels = new JPanel();
    pnlLabels.setOpaque(false);
    pnlLabels.setLayout(new BoxLayout(pnlLabels, BoxLayout.Y_AXIS));

    lblTaskName = new StringDisplay("Task:");
    lblTaskName.add(Box.createHorizontalGlue());
    pnlLabels.add(lblTaskName);

    lblDescription = new StringDisplay("Description:");
    lblDescription.add(Box.createHorizontalGlue());
    pnlLabels.add(lblDescription);

    lblPackage = new StringLinkDisplay("Package:");
    lblPackage.add(Box.createHorizontalGlue());
    pnlLabels.add(lblPackage);

    add(pnlLabels, BorderLayout.CENTER);
  }

  public void setIcon(ImageIcon icon) {
    lblIcon.setIcon(icon);
  }

  public void setTaskDescription(TaskDescription taskDesc) {
    this.taskDescription = taskDesc;

    if (taskDesc == null) {
      lblTaskName.setText("");
      lblDescription.setText("");
      lblDescription.setToolTip("");
      lblPackage.setText("");
      lblPackage.setLink(null);
    }

    else {
     
      //name:
      lblTaskName.setText(taskDesc.getName());

      //description:
      String description = taskDesc.getDescription();
      if (description != null) {

        if (description.length() < 50) {
          lblDescription.setText(description);
        }
        else {
          lblDescription.setText(description.substring(0, 50) + " ...");
          lblDescription.setToolTip(description);
        }
      }

      //link:
      if (taskDesc instanceof ExtendedTaskDescription) {
        ExtendedTaskDescription extDesc = (ExtendedTaskDescription)taskDesc;
        String packageName = extDesc.getPackageName();
        if (packageName != null) {
          lblPackage.setText(packageName);

          String url = extDesc.getWebsite();
          if (url != null)
            lblPackage.setLink(url);
        }
      }
   
    }
  }

  // ------------------------------------------------- Inner Classes

  static class StringDisplay extends JPanel {
    JLabel lblLabel;
    JLabel lblValue;

    public StringDisplay(String name) {
      super(null);
      setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
      setOpaque(false);

      lblLabel = new JLabel(name);
      lblLabel.setFont(lblLabel.getFont().deriveFont(Font.BOLD));
      add(lblLabel);

      add(Box.createHorizontalStrut(8));

      lblValue = new JLabel();
      add(lblValue);
    }

    public void setText(String value) {
      lblValue.setText(value);
    }
    public void setToolTip(String value) {
      lblValue.setToolTipText(value);
    }
  }
  static class StringLinkDisplay extends StringDisplay {

    JClickLabel lblLink;
    String url = null;

    public StringLinkDisplay(String name) {
      super(name);

      lblLink = new JClickLabel();
      lblLink.setForeground(Color.BLUE);    

      lblLink.setActionListener(
          new ActionListener() {
            public void actionPerformed(ActionEvent e) {
              if (url != null)  {
                try {
                  BrowserLauncher.openURL(url.toString());
                }
                catch(Exception ex) {
                  ex.printStackTrace();
                }
              }
            }
          }
      );

      add(Box.createHorizontalStrut(8));
      add(lblLink);
    }

    public void setLink(String url, String displayText) {
      this.url = url;

      if (url == null)
        lblLink.setText("");
      else
        lblLink.setText(displayText);
    }
    public void setLink(String url) {
      setLink(url, url == null ?
          null : "<html><u>(" + url + ")</u></html>");
    }

  }

}
TOP

Related Classes of gri.tasks.gui.TaskHeaderPanel

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.